package com.lemon.exam.repository;

import com.lemon.exam.entity.po.MenuPO;
import com.lemon.exam.entity.vo.MenuVO;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;


/**
 * 菜单表 数据访问接口
 *
 * @author Lemon
 * @since 2025/04/04 14:23:21
 */
public interface MenuRepository extends R2dbcRepository<MenuPO, Long> {
    @Query("""
            SELECT
                id, path, parent_id, name, icon, seq, tree_link_id, create_by, create_time, update_by, update_time, status
            FROM sys_menu ORDER BY seq
            """)
    Flux<MenuVO> findAllMenuVO();

    @Query("""
            SELECT
                id, path, parent_id, name, icon, seq, tree_link_id, create_by, create_time, update_by, update_time, status
            FROM sys_menu
            WHERE id IN (:idList)
            ORDER BY seq
            """)
    Flux<MenuVO> findAllMenuByIdList(List<Long> idList);

    @Query("SELECT id, name, parent_id, status FROM sys_menu WHERE path IS NULL ORDER BY seq")
    Flux<MenuPO> findAllMenuIdMenuName();

    @Query("SELECT path FROM sys_menu WHERE path IS NOT NULL ORDER BY seq")
    Flux<MenuPO> findAllPath();

    @Query("SELECT name, path FROM sys_menu WHERE parent_id = :parentId")
    Flux<MenuPO> findAllByParentId(Long parentId);

    @Query("SELECT name, path FROM sys_menu WHERE parent_id = :parentId AND id <> :id")
    Flux<MenuPO> findAllByIdAndParentId(Long id, Long parentId);

    @Query("SELECT tree_link_id FROM sys_menu WHERE id = :id LIMIT 1")
    Mono<MenuPO> findTreeLinkIdById(Long id);

    @Query("SELECT id, path, parent_id, tree_link_id, name, icon FROM sys_menu ORDER BY seq")
    Flux<MenuVO> findAllByStatus();
}
